<template>
  <div class="apply-body">
    <div class="form-container">
      <h2 style="text-align: center">申请表单</h2>
      <el-form ref="formRef" :model="form" :rules="rules" label-position="top">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入行业" />
        </el-form-item>
        <el-form-item label="通过论坛想了解的内容" prop="context">
          <el-input
            v-model="form.context"
            type="textarea"
            :rows="4"
            placeholder="请输入想了解的内容"
            class="textarea"
          />
        </el-form-item>

        <el-form-item label="参与人员" class="deep" prop="humans" required>
          <div style="flex: 1">
            <div v-for="(participant, index) in form.humans" :key="index" class="participant-item">
              <el-form-item
                :label="`参与人员 ${index + 1}`"
                class="deep"
                :prop="`humans.${index}.name`"
                :rules="participantRules.name"
                style="margin-bottom: 20px"
              >
                <el-input v-model="participant.name" placeholder="姓名" />
              </el-form-item>
              <el-form-item
                :prop="`humans.${index}.position`"
                :rules="participantRules.position"
                style="margin-bottom: 20px"
              >
                <el-input v-model="participant.position" placeholder="职务" />
              </el-form-item>
              <el-form-item :prop="`humans.${index}.phone`" :rules="participantRules.phone">
                <el-input v-model="participant.phone" placeholder="电话" />
              </el-form-item>
              <el-button type="danger" @click="removeParticipant(index)">删除</el-button>
            </div>
            <el-button type="primary" @click="addParticipant">添加参与人员</el-button>
          </div>
        </el-form-item>

        <el-button type="primary" style="width: 100%" size="large" @click="submitForm">
          提交申请
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ForumForm } from "@/api/forum/forum";
import { useForumStoreWithOut } from "@/store/modules/forum/forum";

const useForumStore = useForumStoreWithOut();

const formRef = ref<FormInstance>();

const form = reactive<ForumForm>({
  name: "",
  industry: "",
  context: "",
  humans: [{ name: "", position: "", phone: "" }],
});

const rules = reactive<FormRules>({
  name: [
    { required: true, message: "请输入公司名称", trigger: "blur" },
    { min: 2, max: 50, message: "长度在2到50个字符", trigger: "blur" },
  ],
  industry: [
    { required: true, message: "请输入行业", trigger: "blur" },
    { min: 2, max: 50, message: "长度在2到50个字符", trigger: "blur" },
  ],
  context: [
    { required: true, message: "请输入想了解的内容", trigger: "blur" },
    { min: 10, message: "至少输入10个字符", trigger: "blur" },
  ],
  humans: [
    {
      validator: (_, value, callback) => {
        if (!value || value.length === 0) {
          callback(new Error("至少需要添加一个参与人员"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
});

const participantRules = {
  name: [
    { required: true, message: "请输入姓名", trigger: "blur" },
    { min: 2, max: 10, message: "长度在2到10个字符", trigger: "blur" },
  ],
  position: [
    { required: true, message: "请输入职务", trigger: "blur" },
    { min: 2, max: 20, message: "长度在2到20个字符", trigger: "blur" },
  ],
  phone: [
    { required: true, message: "请输入电话", trigger: "blur" },
    { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" },
  ],
};

const addParticipant = () => {
  form.humans.push({ name: "", position: "", phone: "" });
};

const removeParticipant = (index: number) => {
  ElMessageBox.confirm("确定删除该参与人员吗？", "提示", {
    type: "warning",
  })
    .then(() => {
      form.humans.splice(index, 1);
      ElMessage.success("删除成功");
    })
    .catch(() => {
      ElMessage.info("已取消删除");
      return;
    });
};

const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      await useForumStore.addForum(form);
      ElMessage.success("提交成功");
      formRef.value?.resetFields();
      form.name = "";
    } else {
      console.log("表单验证失败");
    }
  });
};
</script>

<style lang="scss" scoped>
.apply-body {
  overflow: auto;
  background-size: cover;
}

.form-container {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.el-form {
  position: relative;
  z-index: 1;
}

.el-form-item {
  margin-bottom: 24px;
}

.el-form-item__label {
  font-weight: 500;
  color: #333;
}

.participant-item {
  width: 100%;
  margin-bottom: 20px;
  padding: 20px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  position: relative;
  transition: all 0.3s ease;
}

.participant-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transform: translateY(-2px);
}

.participant-item .el-button {
  position: absolute;
  right: 10px;
  top: 10px;
}

.el-button--success {
  background: linear-gradient(135deg, #4ade80 0%, #22c55e 100%);
  border: none;
  font-weight: 500;
  letter-spacing: 1px;
  margin-top: 10px;
}

.el-button--success:hover {
  opacity: 0.9;
}

.el-textarea ::placeholder {
  color: #636569;
  font-weight: bold;
}

::v-deep .el-form-item__label {
  color: #636569;
  font-weight: bold;
}
</style>
